<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/22
 * Time: 11:08
 */

namespace app\service;

use phpmailer\BaseException;
use think\Db;
use think\process\exception\Timeout;

/**
 * 轮播服务层
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class AdverService extends BaseService
{




    /**
     * 数据处理
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param   [array]          $data [广告数据 二维数组]
     */
    public static function DataDealWith($data,$params){

        if(!empty($data) && is_array($data))
        {
            foreach($data as &$v)
            {
                if(is_array($v))
                {
                    if(isset($v['content']))
                    {
                      $v['content']=  ResourceService::ContentStaticReplace($v['content']);
                    }
                    if(isset($v['imgurl']))
                    {

                        $v['imgsrc'] = ResourceService::AttachmentPathViewHandle($v['imgurl'],$params['from']=='client' ? false : true);
                    }
                    //pop_imgurl
                    if(isset($v['pop_imgurl']))
                    {

                        $v['pop_imgsrc'] = ResourceService::AttachmentPathViewHandle($v['pop_imgurl'],$params['from']=='client' ? false : true);
                    }
                    //是否关联商品(列表页显示)
                    if(isset($v['bizpro_type']))
                    {
                        if($v['bizpro_type']==1){
                            $pro=Db::name('bizpro')->where('id='.$v['bizpro_id'])->field('id,name,bizpro_number,sale_price')->find();
                            $v['bizpro_status']=$pro['name'];

                        }else{
                            $v['bizpro_status']='否';
                        }
                    }

                    //是否关联商品(编辑页显示)
                    if(isset($v['bizpro_id'])&&$v['bizpro_id']>0)
                    {

                        $pro=Db::name('bizpro')->where('id='.$v['bizpro_id'])->field('id,name,bizpro_number,sale_price')->find();

                        $v['bizpro_name']=$pro['name'];
                        $v['bizpro_status_number']=$pro['bizpro_number'];
                        $v['bizpro_status_id']=$pro['id'];
                        $v['bizpro_status_price']=$pro['sale_price'];
                    }

                    //上下架时间
                    if(isset($v['start_time'])&&isset($v['end_time']))
                    {
                        //未上架
                        if(TIMESTAMP < $v['start_time'])
                        {
                            $v['status']=1;
                            //已下架
                        }elseif(TIMESTAMP > $v['end_time'])
                        {
                            $v['status']=3;
                        }else{
                            $v['status']=2;
                        }
                    }



                }
            }
        }

        return DataReturn('获取成功',0,$data);
    }

    /**
     * 获取商品数据
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     */
    public static function ProductsStr(){
        $products = Db::table('bizpro')->field('id,name,bizpro_number,sale_price')->where("status = 1")->order('create_time desc')->select();
        if (empty($products))
        {
            throw new BaseException(['code'=>403 ,'errorCode'=>30002,'msg'=>'获取商品失败','status'=>false,'debug'=>false ]);
        }
        return self::DealWithpro($products);
    }

    /**
     * 拼接商品数据表
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param   [array]          $data [商品数据 二维数组]
     */
    public static function DealWithpro($param){
        $_str = '<div class="layui-card-body ">
                        <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>商品名称</th>
                            <th>售价</th>
                            <th>货号</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>';
        foreach ($param as $k => $v)
        {
            $action_detail = '<a class="layui-btn layui-btn-danger layui-btn-radius" onclick="ChooseInfo(this)" >选择</a>';
            $_str .= "<tr style='cursor:pointer' id='trInfo".$v['id']."'>
                            <td>".($k + 1)."</td>
                            <td>".$v['name']."</td>
                            <td>".$v['sale_price']."</td>
                            <td>".$v['bizpro_number']."</td>
                            <td>".$action_detail."</td>
                            </tr>";
        }
        $_str .= '</tbody></table></div>';
        return DataReturn('拼接成功','0',$_str);
    }


    /**
     * 数据校验
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime  2020年7月23日12:06:23
     * @param    [array]          $params [输入参数]
     */
    public static function VaryData($params){
        // 公共数据校验请求参数
        $p = [
            [
                'checked_type' => 'empty',
                'key_name' => 'title',
                'error_msg' => '名称不能为空',
                'error_code' => 30003,
            ],
            [
                'checked_type' => 'empty',
                'key_name' => 'imgurl',
                'error_msg' => '缩略图不能为空',
                'error_code' => 30004,
            ],
        ];

        if($params['advert_type']>1){
            $p[]=[
                'checked_type' => 'empty',
                'key_name' => 'start_time',
                'error_msg' => '上下架时间不能为空',
                'error_code' => 30005,
            ];
        }
        //类型数据校验
        switch ($params['show_type']){
            case 1://弹窗
                $p[]= [
                    'checked_type' => 'empty',
                    'key_name' => 'pop_imgurl',
                    'error_msg' => '弹窗图片不能为空',
                    'error_code' => 30007,
                ];
                break;
            case 2://跳转
                $p[]= [
                    'checked_type' => 'empty',
                    'key_name' => 'jump_address',
                    'error_msg' => '跳转地址不能为空',
                    'error_code' => 30008,
                ];
                break;
            case 3://详情
                $p[]= [
                    'checked_type' => 'empty',
                    'key_name' => 'content',
                    'error_msg' => '图片详情不能为空',
                    'error_code' => 30009,
                ];
                break;
        }
        $ret=ParamsChecked($params, $p);

        if(true !== $ret)
        {
            //$error_arr[0]  $error_arr[1]
            $error_arr = explode(',', $ret);
            throw new BaseException(['code'=>403 ,'errorCode'=>$error_arr[1],'msg'=>$error_arr[0],'status'=>false,'debug'=>false ]);
        }
//        //名称重复判断
//        $check_data = [
//            'where' => [['title', '=', $params['title']],],
//            'table' => 'advert',
//            'params' => $params,
//            'msg' => '名称不能重复',
//            'error_code' => 30001,
//        ];
//        CategoryService::CheckTitle($check_data);


        return true;

    }


    /**
     *数据处理
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月10日11:08:21
     * @param    [array]          $params [输入参数]
     */
    public static function HandleData($params){

        $data = $params;
        //删除多余字段
        unset($data['thumb_img']);
        unset($data['thumb_img1']);
        unset($data['editor_imgs']);
        if(!isset($params['bizpro_type']))
        {
            $data['bizpro_type']=2;
        }
        if(isset($params['start_time']))
        {
            $time_arr=explode(' - ',$params['start_time']);
            $data['start_time']=$time_arr[0];
            $data['end_time']=$time_arr[1];
        }
        if(isset($params['pop_imgurl'])&&!empty($params['pop_imgurl']))
        {
            $data['pop_imgurl']=str_replace('/upload/','',$data['pop_imgurl']);
        }


        $data['imgurl']=str_replace('/upload/','',$data['imgurl']);
        return $data;
    }

    /**
     * 数据保存
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月10日11:08:21
     * @param    [array]          $params [输入参数]
     */
    public static  function SaveAd($data,$params){

        Db::startTrans();
        // 添加/编辑
        if (empty($params['id']))
        {
            $industry_id = Db::name('advert')->insertGetId($data);
            if (!$industry_id) {
                Db::rollback();
                throw new BaseException(['code'=>403 ,'errorCode'=>'30006','msg'=>'添加广告失败','status'=>false,'debug'=>false]);
            }
        } else {
            $data['update_time'] = TIMESTAMP;

            $re = Db::name('advert')->where(['id' => intval($params['id'])])->update($data);
            if (!$re) {
                Db::rollback();
                throw new BaseException(['code' => 403, 'errorCode' => '30007', 'msg' => '修改广告失败', 'status' => false, 'debug' => false]);
            }
        }
        Db::commit();
        return true;
    }


    /**
     * 数据删除
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月23日16:56:53
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function DoDel($param){
        $id = $param['id'];
        $where_del = ['id' => $id];
        $del=Db::name('advert')->where($where_del)->delete();
        if (!$del)
        {
            throw new BaseException(['code'=>403 ,'errorCode'=>'40010','msg'=>'删除广告失败','status'=>false,'debug'=>false]);
        }
        return true;
    }
}